package com.system.core.plugins.mybatis;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
	 @Override
	  public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
	          throws SQLException {
	    ps.setDate(i, Date.valueOf(parameter));
	  }

	  @Override
	  public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
	    Date date = rs.getDate(columnName);
	    return getLocalDate(date);
	  }

	  @Override
	  public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
	    Date date = rs.getDate(columnIndex);
	    return getLocalDate(date);
	  }

	  @Override
	  public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	    Date date = cs.getDate(columnIndex);
	    return getLocalDate(date);
	  }

	  private static LocalDate getLocalDate(Date date) {
	    if (date != null) {
	      return date.toLocalDate();
	    }
	    return null;
	  }
}
